//
//  YKDebugManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/11.
//  Copyright © 2021 edward. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "YKDebugManager.h"
#import "DebugWindow.h"
#import "DebugMainWindow.h"
#import "DebugItemManager.h"
#import "DebugLogWindow.h"
#import "DebugPathLogWindow.h"
#import "DebugNetWorkManager.h"
#import "DebugRabbitMQManager.h"
#import "DebugKeyValue.h"

@interface YKDebugManager ()
///window
@property(nonatomic,strong) DebugWindow *debugWindow;

@end

@implementation YKDebugManager

static YKDebugManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[YKDebugManager alloc] init];
//        [[NSNotificationCenter defaultCenter] addObserver:_instance selector:@selector(mainNotification:) name:kDissMissMainWindow object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:_instance selector:@selector(mainNotification:) name:@"debug_mqmanager" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:_instance selector:@selector(mainNotification:) name:@"debug_logmanager" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:_instance selector:@selector(mainNotification:) name:@"kYKPathDigDEBUGNotification" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:_instance selector:@selector(mainNotification:) name:@"dissMissFount" object:nil];
    }
    return _instance;
}
- (void)install
{
#ifdef DEBUG
    //
    self.debugWindow.hidden = NO;
#else
    
#endif
}

- (DebugWindow *)debugWindow
{
    if (!_debugWindow) {
        _debugWindow = [[DebugWindow alloc]initWithFrame:CGRectMake(0, 360, 40, 40)];
        __weak typeof(self) weakself = self;
        _debugWindow.clickDebugWindow = ^{
            if (weakself.clickBlock) {
                weakself.clickBlock();
            }else
            {
//                [weakself mainNotification:[[NSNotification alloc] initWithName:kDissMissMainWindow object:nil userInfo:nil]];
                [weakself dissMissMainWindow];
            }
        };
    }
    return _debugWindow;
}


- (void)dissMissMainWindow
{
    if ([DebugMainWindow sharedInstance].isHidden) {
        [[DebugMainWindow sharedInstance] show];
    }else
    {
        [[DebugMainWindow sharedInstance] dissMiss];
    }
}

- (void)mainNotification:(NSNotification *)notification
{
    NSString *name = notification.name;
    if ([name isEqualToString:@"debug_mqmanager"]){
        [[DebugRabbitMQManager sharedInstance] saveMQKey:notification.userInfo[@"routingKey"] mqresult:notification.userInfo[@"body"] consumerTag:notification.userInfo[@"consumerTag"] exchangeName:notification.userInfo[@"exchangeName"] receiveDate:notification.userInfo[@"receiveDate"]];
    }else if ([name isEqualToString:@"debug_logmanager"]){
        NSString *logStr = notification.object[@"logStr"]?:@"";
        [YKDebugManager log:logStr];
    }else if ([name isEqualToString:@"kYKPathDigDEBUGNotification"]){
        dispatch_async(dispatch_get_main_queue(), ^{
            [[DebugPathLogWindow sharedInstance] addLogText:[NSString stringWithFormat:@"%@-%@",notification.object[@"point"]?:@"",notification.object[@"name"]?:@""]];
        });
    }else if ([name isEqualToString:@"dissMissFount"]){
//        NSString *
    }
}

- (void)dissMiss
{
    self.debugWindow.hidden = YES;
    self.debugWindow = nil;
}



+ (void)addCustomItem:(UIImage * _Nonnull)itemImage itemTitle:(NSString *)itemTitle itemClickBlock:(void(^)(void(^callBack)(UIViewController *viewController, DebugManagerVCType type, BOOL animated),void(^dissMissMain)(void)))itemClickBlock
{
#ifdef DEBUG
    //
    [[DebugItemManager sharedInstance] addCustomItem:itemImage itemTitle:itemTitle itemClickBlock:itemClickBlock];
#else
#endif
    
}

+ (void)addSwitchItemDetail:(NSString * _Nonnull)switchName switchItemNotificationName:(NSString * _Nonnull)switchItemNotificationName itemClickBlock:(BOOL(^)(BOOL result))itemClickBlock
{
#ifdef DEBUG
    //
    [[DebugItemManager sharedInstance] addSwitchItemDetail:switchName switchItemNotificationName:switchItemNotificationName itemClickBlock:itemClickBlock];
#else
#endif
}

+ (void)log:(NSString *)text
{
#ifdef DEBUG
    //
    dispatch_async(dispatch_get_main_queue(), ^{
        [[DebugLogWindow sharedInstance] addLogText:text];
    });
#else
#endif
}

@end
